MyDesktop.PartnerWindow = Ext.extend(Ext.app.Module, {
    id:'partner-win',
    init : function(){
        this.launcher = {
            text: '销售单位',
            iconCls:'icon-grid',
            handler : this.createWindow,
            scope: this
        }
    },
    partnerRecord : Ext.data.Record.create([
	    {name : 'id',mapping : 'id',type : 'int'}, 	    
	    {name : 'companyName',mapping : 'companyName',type : 'string'}, 
		{name : 'pinyin',mapping : 'pinyin',type : 'string'}, 
	    {name : 'linkTel',mapping : 'linkTel',type : 'String'},
	    {name : 'linkMam',mapping : 'linkMam',type : 'String'},
	    {name : 'address',mapping : 'address',type : 'String'}	   
	   ]),
	cm_partner : new Ext.grid.ColumnModel([
	       new Ext.grid.RowNumberer(),
		   {header : '公司名称', width : 120,dataIndex : 'companyName'},
		   {header : '简拼',width : 120,dataIndex : 'pinyin'},
		   {header : '联系人',width : 120,dataIndex : 'linkMam'},
		   {header : '联系电话',width : 80,dataIndex : 'linkTel'},
		   {header : '联系地址',width : 200,dataIndex : 'address'}
	]),

	getPanel:function(){
		 	 var store=new Ext.data.Store({
				 url : app.url+'/admin/com/partnerList.do',
				 baseParams:{start:0,limit:20},
				 autoLoad : true,
                 reader: new Ext.data.JsonReader({totalProperty : 'totalProperty',root : 'root'}, this.partnerRecord)
             });	
       		 
       		 var partner_detailForm = new Ext.FormPanel({
       		 	 labelSeparator : "：",
				 frame:true,
				 border:false,
				 defaultType : 'textfield',
				 defaults:{width:160},
				 items:[{
			              xtype:'hidden',
			              name:'id',
						  value:'0'
			            },{				     	
				      	fieldLabel:'公司名称',
				      	name:"companyName",
						allowBlank : false,
						 minLength : 2,
			             maxLength : 50
				        },{				     	
				     	fieldLabel:'联系人',
				      	name:"linkMam",
			            maxLength : 20
				        },{
				      	fieldLabel:'联系电话',
				     	name:"linkTel",
			            maxLength : 30
				        },{
				      	fieldLabel:'联系地址',
				     	name:"address",
						maxLength : 50
				       }],
				 buttons:[
			       {
			     	id:'commitButton',
					iconCls : 'save',
			        handler:submitForm
			       },{
			     	 id:'closeButton',
					 text : '关闭',
					 iconCls : 'close',
			     	 handler:function(){				     		
			     	    addOrEditPartner_win.hide();
			     	}
			      }
			     ]
       		 });
       		 
       		 var addOrEditPartner_win = new Ext.Window({
       		 	layout:'fit',
		    	width:330,
		    	closeAction:'hide',
		   		height:200,
				resizable :false,
				shadow : true,
				modal :true,
		   	    closable:true,
		    	bodyStyle:'padding:5 5 5 5',		    	
		    	animCollapse:true,
				items:[partner_detailForm]
       		 });
       		 
       		 var partner_gridPanel =  new Ext.grid.GridPanel({					  
                        border:false,
					    columnLines :true,
                        enableHdMenu : false,
						loadMask : {msg : '数据加载中...'},
                        ds: store,
                        cm: this.cm_partner,						
                        viewConfig: {
                            forceFit:false
                        },
                        tbar:[{
                            text:'增加',
                            tooltip:'增加',
                            iconCls:'add',
                            handler:addPartner
                        }, '-', {
                            text:'修改',
                            tooltip:'修改',
                            iconCls:'option',
                            handler:editPartner
                        },'-',{
                            text:'删除',
                            tooltip:'删除',
                            iconCls:'delete'
                        }],
						bbar: new Ext.PagingToolbar({
                            pageSize: 20,
                            store: store,
                            displayInfo: true,
                            plugins: new Ext.ux.ProgressBarPager()
                        })
            });
                      
            
            
              //取得选中的记录
	 		function getSelectedItem(){
	     		var record= partner_gridPanel.getSelectionModel().getSelected();	   
	     		if(record == null){
	        		 Ext.MessageBox.alert('提示','请选择要修改的明细！');
	         		return false;
	     		}else{
	        		return record;
	     		}
	  		}
            
            function addPartner(){
            	if(partner_detailForm.form.el){
            		partner_detailForm.form.el.dom.reset();            	
            	}else{
            		partner_detailForm.form.reset();
            	}
                Ext.getCmp('commitButton').setText('新增');
		        addOrEditPartner_win.isAdd = true;
		        addOrEditPartner_win.setTitle("新增合作单位");
		        addOrEditPartner_win.show();
            }
            
            function editPartner(){
            	if(partner_detailForm.form.el){
            		partner_detailForm.form.el.dom.reset();            	
            	}else{
            		partner_detailForm.form.reset();
            	}
            	var record=getSelectedItem();
            	if(record){
            		Ext.getCmp('commitButton').setText('修改');
		    		addOrEditPartner_win.setTitle("编辑合作单位");
		    		addOrEditPartner_win.isAdd = false;
		    		partner_detailForm.getForm().loadRecord(record);
		    		addOrEditPartner_win.show();
            	}else{
            		 Ext.MessageBox.alert('提示','请选择要修改的明细！');
            	}           	
            }
            
            function submitForm(){
			   var form=partner_detailForm.getForm();
            	if(!form.isValid()){
	         		 return;
	      		}
	      		if(addOrEditPartner_win.isAdd){
				    form.findField('id').setValue(0); //setRawValue(0);
	      		  	Ext.Ajax.request({
	           			url:app.url+'/admin/com/createPartner.do',
	           			method :"post",
						jsonData :Ext.encode(form.getValues()),
						waitMsg : '正在提交数据,请稍候...',
	          			success:function(response,options){
						
							store.reload();
							Ext.MessageBox.alert("提示","操作成功!");
							addOrEditPartner_win.hide();
	           			},
	           			failure:function(response,options){
	                		Ext.MessageBox.alert("提示","发生异常，请稍候处理!");    
	           			}
	       			});
	      		}else{
            		Ext.Ajax.request({
	           			url:app.url+'/admin/com/updatePartner.do',
	           			method :"post",
						jsonData :Ext.encode(form.getValues()),
						waitMsg : '正在提交数据,请稍候...',
	          			success:function(response,options){
						    store.reload();
							Ext.MessageBox.alert("提示","修改成功!");
							addOrEditPartner_win.hide();
	           			},
	           			failure:function(response,options){
	                		Ext.MessageBox.alert("提示","发生异常，请稍候处理!");    
	           			}
	       			});
           		}
         	}
         	
         	return partner_gridPanel;
      },
    createWindow : function(){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('partner-win');
        if(!win){
       
            win = desktop.createWindow({
                id: 'partner-win',
                title:'客户管理',
                width:740,
                height:480,
                iconCls: 'icon-grid',
                shim:false,
                animCollapse:false,
                constrainHeader:true,

                layout: 'fit',
                items:this.getPanel()
                   
                  
            });
        }
        win.show();
    }
});
